﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.ComponentModel;

namespace ajayumi.develop.win32.Transport.Server.WinForm
{
    /// <summary>
    /// FaultFile.cs 故障文件操作类
    /// Author   : 黄乙冬
    /// Date     : 2011-11-09
    /// </summary>
    internal class FaultFile : IDisposable
    {
        public bool HasRecord { get; private set; }
        public Exception Error { get; private set; }
        public string FilePath { get; private set; }
        public Action<FaultFile> AfterCompleted { get; set; }

        public FaultFile(string path)
        {
            this.FilePath = path;
        }

        internal void Start()
        {
            Action action = new Action(this.Analyse);
            action.BeginInvoke(new AsyncCallback(this.AnalyseCallback), action);
        }

        /// <summary>
        /// 分析故障
        /// </summary>
        private void Analyse()
        {
            try
            {
                using (StreamReader sr = new StreamReader(this.FilePath))
                {
                    int index = 1;
                    while (sr.Peek() >= 0)
                    {
                        string line = sr.ReadLine();
                        if (string.IsNullOrEmpty(line))
                        { continue; }

                        //Match match = Regex.Match(line, @"\[(?<devCode>.*)\]\s+\[(?<fauDate>.*)\]\s+\[(?<fauData>.*)\]", RegexOptions.IgnoreCase);

                        //string devCode = match.Groups["devCode"].Value.Trim();
                        //DateTime fauDate = DateTime.Now;
                        //DateTime.TryParse(match.Groups["fauDate"].Value, out fauDate);
                        //string fauData = match.Groups["fauData"].Value.Trim();
                        //string fauCode = Regex.Match(fauData, @"..\s..\s(?<fauCode>.{1,56})").Groups["fauCode"].Value.Replace(" ", string.Empty);

                        //bool isVali = true;
                        ////写入数据库
                        //Dev_Fau devFau = new Dev_Fau()
                        //{
                        //    Row_id = string.Concat(DateTime.Now.ToString("yyMMddhhmmss"), index++),
                        //    Created = DateTime.Now,
                        //    Last_upd = DateTime.Now,
                        //    Fau_code = fauCode,
                        //    Fau_data = fauData,
                        //    Fau_date = fauDate,
                        //    Fau_source = DevFauManager.FAUSOURCE_REMOTE,
                        //    Fau_status = DevFauManager.FAUSTATUS_UNPROCESSED,
                        //    Satisfaction = DevFauManager.SATISFACTION_NORMAL,
                        //    Feedback_date = DateTime.Now,
                        //    Status = true
                        //};
                        //Dev_info devInfo = DevInfoManager.Instance.GetModelByDevCode(devCode);
                        //if (devInfo != null)
                        //{
                        //    devFau.Devinfo_id = devInfo.Row_id;
                        //}
                        //else
                        //{
                        //    //没有找到对应的设备
                        //    isVali = false;
                        //}
                        //Fau_code_maintain fauCodeMaintain = FauCodeManager.Instance.GetModelByFauCode(fauCode);
                        //if (fauCodeMaintain != null)
                        //{
                        //    devFau.Faucode_id = fauCodeMaintain.Row_id;
                        //}
                        //else
                        //{
                        //    //没有找到对应的故障记录
                        //    isVali = false;
                        //}
                        //Hosp_Dev hospDev = HospDevManager.Instance.GetModelByDevinfoId(devInfo.Row_id);
                        //if (hospDev != null)
                        //{
                        //    devFau.Hospinfo_id = hospDev.Hospinfo_id;
                        //}
                        //else
                        //{
                        //    //没有找到对应的医院
                        //    isVali = false;
                        //}

                        //this.HasRecord = isVali;
                        //DevFauManager.Instance.Add(devFau);
                    }
                }
            }
            catch (Exception ex)
            {
                this.Error = ex;
                Console.WriteLine("读取故障日志发生错误");
            }
        }

        private void AnalyseCallback(IAsyncResult ar)
        {
            Action action = ar.AsyncState as Action;
            action.EndInvoke(ar);
            this.AfterCompleted(this);
        }

        public void Dispose()
        {

        }
    }
}
